package com.conncui.subscript.persenter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.conncui.common.tool.ToolLog;
import com.conncui.common.tool.Utility;
import com.conncui.subscript.DragPointView;
import com.conncui.subscript.OnAddFinishedListener;
import com.conncui.subscript.R;
import com.conncui.subscript.SubscriptDrawable;
import com.conncui.subscript.SubscriptHelper;
import com.conncui.subscript.adapter.ConstantFlag;
import com.conncui.subscript.adapter.OverLayAdapterV2;
import com.conncui.subscript.location.AnchorLocation;
import com.conncui.subscript.location.RelativePosition;

import android.app.ActionBar;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v4.view.MotionEventCompat;
import android.support.v4.view.PointerIconCompat;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewTreeObserver.OnPreDrawListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ShowPresenterV2 {
    final int LIST_ITEM_COUNT;
    final String TAG;
    ActionBar mActionBar;

    SubscriptDrawable mActionBarSubscripts;
    View mActionBatHostView;
    View mActionBatRelativeView;
    Button mAddDrawableBtn;
    Activity mAttachActivity;
    View mContentView;
    List<Map<String, Object>> mDataListObj;
    ImageView mImageView;
    EditText mInputTextEt;
    SubscriptDrawable mLeBottomWidgetSubscripts;
    OnClickListener mOnClickListener;
    OverLayAdapterV2 mOverLayAdapterV2;
    ListView mOverLayLv;
    Button mPickColorBtn;
    Button mRemoveDrawableBtn;
    Resources mResources;
    SubscriptDrawable mSubscriptDrawable;
    TextWatcher mTextWatcher;

    DragPointView dragpointview;

    LinearLayout mHostLL;

    class NormalViewOverlay implements Runnable {
        final Drawable drawableBack;
        final Drawable drawableWifi;

        NormalViewOverlay(Drawable drawable, Drawable drawable2) {
            this.drawableBack = drawable;
            this.drawableWifi = drawable2;
        }

        public void run() {
            this.drawableBack.setBounds(ShowPresenterV2.this.mImageView.getLeft(), ShowPresenterV2.this.mImageView.getTop(), ShowPresenterV2.this.mImageView.getRight(), ShowPresenterV2.this.mImageView.getBottom());
            this.drawableWifi.setBounds(ShowPresenterV2.this.mImageView.getLeft(), ShowPresenterV2.this.mImageView.getTop(), ShowPresenterV2.this.mImageView.getRight(), ShowPresenterV2.this.mImageView.getBottom());
            ShowPresenterV2.this.mImageView.getOverlay().add(this.drawableWifi);
            ShowPresenterV2.this.mImageView.getOverlay().add(this.drawableBack);
            testViewGroup();

        }
    }

    class ContentTextWatcher implements TextWatcher {
        ContentTextWatcher() {
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        public void afterTextChanged(Editable s) {
            ShowPresenterV2.this.mActionBarSubscripts.setText(s.toString());
            ShowPresenterV2.this.mLeBottomWidgetSubscripts.setText(s.toString());
            if (ShowPresenterV2.this.mSubscriptDrawable != null) {
                ShowPresenterV2.this.mSubscriptDrawable.setText(s.toString());
            }
        }
    }

    class OperateClickListener implements OnClickListener {
        OperateClickListener() {
        }

        public void onClick(View v) {
            switch (v.getId()) {

                case R.id.addDrawableBtn /*2131820857*/:
                    ShowPresenterV2.this.testActionBarAddSubscript();
                case R.id.removeDrawableBtn /*2131820858*/:
                    ShowPresenterV2.this.mActionBatRelativeView.getOverlay().remove(ShowPresenterV2.this.mActionBarSubscripts);
                    ShowPresenterV2.this.mActionBatRelativeView.invalidate();
                case R.id.subscript_pick_color /*2131820859*/:
                    if (ShowPresenterV2.this.mSubscriptDrawable != null) {
                        ShowPresenterV2.this.mSubscriptDrawable.setTextColor(Color.argb(MotionEventCompat.ACTION_MASK, (int) (Math.random() * 255.0d), (int) (Math.random() * 255.0d), (int) (Math.random() * 255.0d)));
                        ShowPresenterV2.this.mSubscriptDrawable.setBgColor(Color.argb(MotionEventCompat.ACTION_MASK, (int) (Math.random() * 255.0d), (int) (Math.random() * 255.0d), (int) (Math.random() * 255.0d)));
                    }
                default:
            }
        }
    }

    class ActionBarPreDrawListener implements OnPreDrawListener {
        ActionBarPreDrawListener() {
        }

        public boolean onPreDraw() {
            SubscriptHelper
                    .add(ShowPresenterV2.this.mActionBatRelativeView, ShowPresenterV2.this.mActionBatRelativeView, new AnchorLocation(0.5f, 0.0f), ShowPresenterV2.this.mActionBarSubscripts, new AnchorLocation(0.0f, 0.0f), new RelativePosition(0.0f, 0.0f));
            ShowPresenterV2.this.mActionBatRelativeView.getViewTreeObserver().removeOnPreDrawListener(this);
            ShowPresenterV2.this.mActionBatRelativeView.invalidate();
            return true;
        }
    }

    class BottomSheetPreDrawListener implements OnPreDrawListener {
        BottomSheetPreDrawListener() {
        }

        public boolean onPreDraw() {
            return true;
        }
    }

    class OverLayAddFinishedListener implements OnAddFinishedListener {
        OverLayAddFinishedListener() {
        }

        public void onAddFinished(Drawable drawable) {
            if (drawable != null && (drawable instanceof SubscriptDrawable)) {
            }
        }
    }

    public ShowPresenterV2(Activity attachActivity, View contentView) {
        this.TAG = getClass().getSimpleName();
        this.LIST_ITEM_COUNT = PointerIconCompat.TYPE_DEFAULT;
        this.mTextWatcher = new ContentTextWatcher();
        this.mOnClickListener = new OperateClickListener();
        this.mAttachActivity = attachActivity;
        this.mResources = attachActivity.getResources();
        this.mContentView = contentView;
    }

    public void createView() {
        this.mImageView = (ImageView) this.mContentView.findViewById(R.id.imageView);
        this.mInputTextEt = (EditText) this.mContentView.findViewById(R.id.inputTextEt);
        this.mRemoveDrawableBtn = (Button) this.mContentView.findViewById(R.id.removeDrawableBtn);
        this.mAddDrawableBtn = (Button) this.mContentView.findViewById(R.id.addDrawableBtn);
        this.mPickColorBtn = (Button) this.mContentView.findViewById(R.id.subscript_pick_color);
        this.mOverLayLv = (ListView) this.mContentView.findViewById(R.id.overLayAtItem);
        this.dragpointview = (DragPointView) this.mContentView.findViewById(R.id.dragpointview);
        this.mInputTextEt.addTextChangedListener(this.mTextWatcher);
        this.mRemoveDrawableBtn.setOnClickListener(this.mOnClickListener);
        this.mAddDrawableBtn.setOnClickListener(this.mOnClickListener);
        this.mPickColorBtn.setOnClickListener(this.mOnClickListener);
        this.mHostLL = (LinearLayout) this.mContentView.findViewById(R.id.hostLL);
        initActionBar();

        ToolLog.d(this.TAG, "6dp:" + this.mResources.getDimension(R.dimen.le_subscripts_view_outside_drawableR_margin_relativeL_6dp) + " px");
    }

    public void onResume() {
        testListView();
        this.mImageView.post(new NormalViewOverlay(this.mResources.getDrawable(R.drawable.back, null), this.mResources.getDrawable(R.drawable.select, null)));
    }

    private List<Map<String, Object>> getListForBaseAdapter() {
        this.mDataListObj = new ArrayList();
        for (int i = 0; i < PointerIconCompat.TYPE_DEFAULT; i++) {
            HashMap<String, Object> tHashMap = new HashMap();
            tHashMap.put(ConstantFlag.KEY_ImageButton, Integer.valueOf(R.drawable.imagebutton));
            tHashMap.put(ConstantFlag.KEY_ZoomButton, Integer.valueOf(R.drawable.zoombutton));
            tHashMap.put(ConstantFlag.KEY_Button, Integer.valueOf(R.drawable.delete));
            tHashMap.put(ConstantFlag.KEY_TextView, "TV" + i);
            tHashMap.put(ConstantFlag.KEY_CheckBox, Boolean.valueOf(i % 2 == 0));
            this.mDataListObj.add(tHashMap);
        }
        return this.mDataListObj;
    }

    void initActionBar() {
        this.mActionBar = this.mAttachActivity.getActionBar();
        this.mActionBar.show();
        this.mActionBarSubscripts = new SubscriptDrawable(this.mAttachActivity, new AnchorLocation(0.0f, 0.0f));
        this.mActionBarSubscripts.setText("actionBar");
    }


    void testActionBarAddSubscript() {
        this.mActionBatHostView = this.mAttachActivity.getWindow().getDecorView().findViewById(this.mResources.getIdentifier("action_bar", "id", "android"));
        this.mActionBatRelativeView = this.mActionBatHostView.findViewById(R.id.save);
        this.mActionBatRelativeView.getViewTreeObserver().addOnPreDrawListener(new ActionBarPreDrawListener());
    }


    void testListView() {
        this.mOverLayAdapterV2 = new OverLayAdapterV2(this.mAttachActivity, getListForBaseAdapter());
        this.mOverLayLv.setAdapter(this.mOverLayAdapterV2);
    }

    void testViewGroup(){

        final View view = null;
        final ViewGroup viewGroup = null;
        final Drawable drawable = null;
//
//        view.getViewTreeObserver().addOnPreDrawListener(
//                new OnPreDrawListener() {
//            @Override
//            public boolean onPreDraw() {
//                drawable.setBounds(Rect rect);
//
//                view.getOverlay().add(Drawable drawable);
//                viewGroup.getOverlay().add(Drawable drawable);
//                viewGroup.getOverlay().add(View view);
//                return true;
//            }
//        });
        mImageView.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
//                mImageView.getOverlay().add();
                ImageView addView = new ImageView(mHostLL.getContext());
                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mImageView.getLayoutParams();
                params.weight = 500;
                params.height = 500;
                addView.setImageResource(R.drawable.android);
                addView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Utility.showToast(mContentView.getContext(),"dddd");
                    }
                });
                addView.setLayoutParams(params);
//                (mHostLL).getOverlay().add(textView);
                (mHostLL).removeView(mImageView);
                (mHostLL).addView(addView,1,params);
                return true;
            }
        });
    }

    public View getContentView() {
        return this.mContentView;
    }
}
